class ComosuposController < ApplicationController

  before_filter :login_required
  require_role 'marketing'

  def index
    store_location
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    store_location
    @actuales = Comosupo.find_not_deleted(:all)
    @eliminados = Comosupo.find_deleted(:all)
    @comosupos= Comosupo.find(:all)
    @year = Date.today.year
    @ranking_comosupos=Array::new(12){|mes| Visita.find(:all,:select=>"comosupo_id,count(*) as cant",:conditions=>"MONTH(fecha)=#{mes+1} and YEAR(fecha)=#{@year}",:group=>"comosupo_id", :order=>"cant DESC")}    
  end

  def new
    @comosupo = Comosupo.new
  end

  def create
    @comosupo = Comosupo.new(params[:comosupo])
    if @comosupo.save
      flash[:success] = 'Comosupo was successfully created.'
      redirect_back_or_default :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @comosupo = Comosupo.find(params[:id])
  end

  def update
    @comosupo = Comosupo.find(params[:id])
    if @comosupo.update_attributes(params[:comosupo])
      flash[:success] = 'Comosupo was successfully updated.'
      redirect_back_or_default :action => 'show', :id => @comosupo
    else
      render :action => 'edit'
    end
  end

  def delete
    c=Comosupo.find(params[:id])
    c.deleted=true
    c.save
    redirect_back_or_default :action => 'list'
  end
  
  def restore
    c=Comosupo.find(params[:id])
    c.deleted=false
    c.save
    redirect_back_or_default :action => 'list'
  end

  def destroy
    Comosupo.find(params[:id]).destroy
    redirect_back_or_default :action => 'list'
  end
end
